package cn.oms.auth.provider.controller;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/home/api")
@Slf4j
@Api(tags = "home API接口")
public class HomeController {
    //Spring-Security框架判断资格时,需要使用下面的类型常量
    public static final GrantedAuthority PATIENT= new SimpleGrantedAuthority("ROLE_PATIENT");
    public static final GrantedAuthority NURSE= new SimpleGrantedAuthority("ROLE_NURSE");
    public static final GrantedAuthority BILLING= new SimpleGrantedAuthority("ROLE_BILLING");
    public static final GrantedAuthority DOCTOR= new SimpleGrantedAuthority("ROLE_DOCTOR");
    public static final GrantedAuthority PHARMACIST= new SimpleGrantedAuthority("ROLE_PHARMACIST");
    public static final GrantedAuthority ADMINISTRATORS= new SimpleGrantedAuthority("ROLE_ADMINISTRATORS");

    // /auth/api/home 访问这个方法
    @GetMapping
    public String index(@AuthenticationPrincipal UserDetails user){
        //判断当前登录用户是否包含讲师身份
        if(user.getAuthorities().contains(PATIENT)){
            //跳转到患者首页 重定向到患者首页
            return "/user/patient/index_patient.html";
        }else if(user.getAuthorities().contains(BILLING)){
            //如果是收银 重定向到收银首页
            return "/user/billing/index_billing.html";
        }else if(user.getAuthorities().contains(NURSE)){
            //如果是护士 重定向到护士首页
            return "/user/nurse/index_nurse.html";
        }else if(user.getAuthorities().contains(DOCTOR)){
            //如果是医生 重定向到医生首页
            return "/user/doctor/index_doctor.html";
        }else if(user.getAuthorities().contains(PHARMACIST)){
            //如果是药剂师 重定向到药剂师首页
            return "/user/pharmacy/index_pharmacist.html";
        }else if (user.getAuthorities().contains(ADMINISTRATORS)){
            //如果是超级管理员 重定向到超级管理员首页
            return "/user/admin/index_administrators.html";
        }
        //既不是患者又不是医生又不是管理员,去登录
        return "/login.html";
    }


}
